import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter)

const router = new VueRouter({
  routes:[
    { path:'/login',component:() => import('@/components/Login.vue') },
    { 
      path:'/',
      redirect:'/user',
      component:() => import('@/components/Home.vue'),
      children:[
        { path:'/user',component:() => import('@/components/menus/User.vue') },
        { path:'/good',component:() => import('@/components/menus/Good.vue') },
        { path:'/order',component:() => import('@/components/menus/Order.vue') },
        { path:'/right',component:() => import('@/components/menus/Right.vue') },
        { path:'/setting',component:() => import('@/components/menus/Setting.vue') },
        { path:'/userDetail/:id',component:() => import('@/components/user/UserDetail.vue') },
      ]
    }
  ]
})

// 导航的全局前置守卫
router.beforeEach((to,from,next) => {
  // 判断是不是白名单登录页面
  if (to.path === '/login'){
    // 是，放行
    next()
  }else {
    const token = localStorage.getItem('token')
    if (token) {
      // 有token存在，证明已经登录过，可以放行
      next()
    }else {
      // 无token存在，不能放行
      next('/login')
    }
  }
})

export default router